/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.button;

import java.awt.Dimension;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.button
 * @date 27/03/2014 - 18:31:59
 */
abstract class ActionButton extends JButton {
	
	private static final long serialVersionUID = -8756876167032428076L;
	
	private static final String ICON_FULL_NAME = "/org/jxSwing/resources/icon/action/:NAME-:SIZE.png";
	
	private ImageIcon DEFAULT_ICON = null;
	
	/**
	 * @param iconName
	 *        Example: add
	 * @param iconSize
	 *        Example: 32x32
	 * @param buttonSize
	 *        Example: new Dimension(25, 25)
	 */
	public ActionButton(final String iconName, final String iconDimension, final Dimension buttonSize) {
	
		this(iconName, iconDimension, null, buttonSize);
	}
	
	/**
	 * @param iconName
	 *        Example: add
	 * @param iconSize
	 *        Example: 32x32
	 * @param text
	 * @param buttonSize
	 *        Example: new Dimension(25, 25)
	 */
	public ActionButton(final String iconName, final String iconDimension, final String text, final Dimension buttonSize) {
	
		DEFAULT_ICON = getImageIcon(iconName, iconDimension);
		
		setIcon(getDefaultIcon());
		setSize(buttonSize);
		setText(text);
	}
	
	/**
	 * @return the DEFAULT_ICON
	 */
	protected final ImageIcon getDefaultIcon() {
	
		return DEFAULT_ICON;
	}
	
	/**
	 * @param iconName
	 *        Example: add
	 * @param iconSize
	 *        Example: 32x32
	 * @return fullIconName Example: /org/jxSwing/resources/icon/action/<b>add</b>-<b>32x32</b>.png
	 */
	protected final String getFullIconName(final String iconName, final String iconDimension) {
	
		return ICON_FULL_NAME.replace(":NAME", iconName).replace(":SIZE", iconDimension);
	}
	
	/**
	 * @param iconName
	 *        Example: add
	 * @param iconSize
	 *        Example: 32x32
	 * @return urlIconResource
	 */
	protected final URL getIconResource(final String iconName, final String iconDimension) {
	
		return ActionButton.class.getResource(getFullIconName(iconName, iconDimension));
	}
	
	/**
	 * @param iconName
	 *        Example: add
	 * @param iconSize
	 *        Example: 32x32
	 * @return imageIcon
	 */
	protected final ImageIcon getImageIcon(final String iconName, final String iconDimension) {
	
		return new ImageIcon(getIconResource(iconName, iconDimension));
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.Component#setSize(int, int)
	 */
	@Override
	public void setSize(final int width, final int height) {
	
		final Dimension dimension = new Dimension(width, height);
		
		super.setSize(width, height);
		super.setPreferredSize(dimension);
		super.setMinimumSize(dimension);
		super.setMaximumSize(dimension);
	}
	
	/**
	 * @param visible
	 */
	public void setVisibleText(final boolean visible) {
	
		setText(getToolTipText());
	}
}
